package cn.gogetter;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.gogetter.entity.KeyInfo;
import cn.gogetter.mapper.KeyInfoMapper;
import cn.gogetter.pojo.RetResponse;
import cn.gogetter.pojo.RetResult;
import lombok.extern.java.Log;

@Log
@RestController
@CrossOrigin
@RequestMapping("/api")
public class APIController {
	
	@Resource
	private KeyInfoMapper keyInfoMapper;
	@Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/saveKey",method = RequestMethod.POST)
    public RetResult<Map> saveKey(@RequestParam(value="kname",required=true) String kname,
    		@RequestParam(value="mac",required=true) String mac,
    		@RequestParam(value="cpu",required=true) String cpu,
    		@RequestParam(value="boss",required=true) String boss) {
    	
    	Date createdate = new Date();
    	Calendar calendar = Calendar.getInstance(); 
	  	calendar.setTime(createdate);
	  	calendar.add(Calendar.YEAR,1);
	  	Date offdate = calendar.getTime();
	  	KeyInfo keyInfo = new KeyInfo();
	  	keyInfo.setKname(kname);
	  	keyInfo.setMac(mac);
	  	keyInfo.setCpu(cpu);
	  	keyInfo.setBoss(boss);
	  	keyInfo.setCreatedate(createdate);
	  	keyInfo.setOffdate(offdate);
	  	log.info(keyInfo.toString());
	  	Map<String, Object> resultData = new HashMap<String, Object>();
	  	resultData.put("count", keyInfoMapper.insert(keyInfo));
	  	resultData.put("id", keyInfo.getId());
        return RetResponse.makeOKRsp(resultData);
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        return "Spring Boot Test Demo!";
    }
}
